package com.sx.zongzhi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.SpinnerPubAdapter;
import com.sx.zongzhi.bean.EventTypeBean;
import com.sx.zongzhi.bean.GlobalBean;
import com.sx.zongzhi.bean.PublicBean;
import com.sx.zongzhi.presenter.PublicListPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IPublicBeanView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 矛盾纠纷查询
 */
public class DisputeSearchActivity extends BaseActivity implements IPublicBeanView {

    @BindView(R.id.et_EventName)
    EditText et_EventName;
    @BindView(R.id.sp_EventScale)
    Spinner scaleSpinner;
    @BindView(R.id.sp_EventSource)
    Spinner sourceSpinner;
    @BindView(R.id.tv_EventType)
    TextView tv_EventType;
    @BindView(R.id.tv_StartTime)
    TextView tv_StartTime;
    @BindView(R.id.tv_EndTime)
    TextView tv_EndTime;
    //事件性质
    private List<GlobalBean> eventScaleList;
    //事件来源
    private List<GlobalBean> eventSourceList;
    private PublicListPest publicListPest;
    private String eventTypeID="";
    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_dispute_search);
        setTitleName("矛盾纠纷查询");
        setTitleRight(R.string.search);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        publicListPest=new PublicListPest(this);
        publicListPest.getPublicList("事件性质");//事件规模
        publicListPest.getPublicList("事件来源方式");//事件来源
    }


    @OnClick({R.id.tv_right,R.id.tv_StartTime,R.id.tv_EndTime,R.id.tv_EventType})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right:
                submit();
                break;
            case R.id.tv_StartTime:
                BaseUtil.createDataPop(this,tv_StartTime);
                break;
            case R.id.tv_EndTime:
                BaseUtil.createDataPop(this,tv_EndTime);
                break;
            case R.id.tv_EventType:
                startActivityForResult(new Intent(DisputeSearchActivity.this,EventTypeActivity.class),4446);
                break;
        }
    }

    private void submit() {
        String name=et_EventName.getText().toString().trim();
        String scaleId="";
        GlobalBean scale=(GlobalBean)scaleSpinner.getSelectedItem();
        if (scale!=null){
            scaleId=(scale.getId()).equals("-1") ?"":scale.getId();
        }
        String sourceId="";
        GlobalBean source=(GlobalBean)sourceSpinner.getSelectedItem();
        if (source!=null){
            sourceId=(source.getId()).equals("-1") ?"":source.getId();
        }
        String start=tv_StartTime.getText().toString();
        String end=tv_EndTime.getText().toString();
        Intent intent=new Intent();
        intent.putExtra("EventName",name);
        intent.putExtra("EventScale",scaleId);
        intent.putExtra("EventSource",sourceId);
        intent.putExtra("EventType",eventTypeID);
        intent.putExtra("StartTime",start);
        intent.putExtra("EndTime",end);
        setResult(RESULT_OK,intent);
        finish();

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 4446://选择事件类别
                    EventTypeBean.TypesBean typesBean= (EventTypeBean.TypesBean) data.getSerializableExtra("EventTypeBeanID");
                    tv_EventType.setText(data.getStringExtra("PrentName")+"-"+typesBean.getTypeName());
                    eventTypeID=typesBean.getTypeId();
                    break;
            }
        }
    }

    @Override
    public void showError(String message) {
        ToastUtils.showToast(this,message);
    }

    @Override
    public void getPublicList(PublicBean publicBean) {
        if ("事件性质".equals(publicBean.getDomainName())){
            eventScaleList=publicBean.getDicts();
            eventScaleList.add(0,new GlobalBean("未选择","-1"));
            if (eventScaleList!=null){
                SpinnerPubAdapter adapter=new SpinnerPubAdapter(this,R.layout.item_text,eventScaleList);
                scaleSpinner.setAdapter(adapter);
            }
        }
        if ("事件来源方式".equals(publicBean.getDomainName())){
            eventSourceList=publicBean.getDicts();
            eventSourceList.add(0,new GlobalBean("未选择","-1"));
            if (eventSourceList!=null){
                SpinnerPubAdapter adapter=new SpinnerPubAdapter(this,R.layout.item_text,eventSourceList);
                sourceSpinner.setAdapter(adapter);
            }
        }
    }
}
